<template>
  <div class="classManage" style="padding: 15px">
    <!-- 查询框 -->
    <el-form :model="selectClass" ref="selectClass" class="demo-dialogForm" v-show="toggle"
             inline>
      <el-form-item label="班级名称" prop="name" size="medium">
        <el-input v-model="selectClass.name" placeholder="请输入班级名称"></el-input>
      </el-form-item>
      <el-form-item label="班级代码" prop="code" size="medium">
        <el-input v-model="selectClass.code" placeholder="请输入班级代码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="selectButton" size="mini">搜索
        </el-button>
        <el-button icon="el-icon-refresh" @click="reset" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮行 -->
    <el-row>
      <el-button icon="el-icon-plus" plain type="primary" @click="insertHandler" size="mini">新增</el-button>
      <el-button icon="el-icon-delete" plain type="danger" @click="deleteHandler" size="mini">删除</el-button>
      <div class="right-toolbar">
        <el-tooltip class="item" effect="dark" :content="toggle?'隐藏搜索':'显示搜索'" placement="top">
          <el-button size="mini" circle icon="el-icon-search" @click="toggle=!toggle"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark" content="刷新" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="pageListClass"/>
        </el-tooltip>
      </div>
    </el-row>

    <!-- 列表 -->
    <el-table :data="classes" tooltip-effect="dark" style="width: 100%;margin-top: 15px" :header-cell-style="style"
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection"></el-table-column>
      <el-table-column align="center" prop="id" label="序号" width="60"></el-table-column>
      <el-table-column align="center" prop="name" label="班级名称"></el-table-column>
      <el-table-column align="center" prop="code" label="班级代码"></el-table-column>
      <el-table-column align="center" label="操作" width="160">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" size="mini" @click="updateRow(scope.row)">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="mini" @click="deleteRow(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="800px">
      <!--ref相当于id-->
      <el-form :model="dialogForm" ref="dialogForm" label-width="100px" inline>
        <el-form-item label="班级名称" prop="name" placeholder="请输入班级名称">
          <el-input v-model="dialogForm.name" style="width: 250px"></el-input>
        </el-form-item>
        <el-form-item label="班级代码" prop="code" placeholder="请输入班级代码">
          <el-input v-model="dialogForm.code" style="width: 250px"></el-input>
        </el-form-item>
      </el-form>
      <el-divider>学生信息</el-divider>
      <el-row>
        <el-button icon="el-icon-plus" plain type="primary" @click="insertStudentHandler"
                   size="mini">新增
        </el-button>
        <el-button icon="el-icon-delete" plain type="danger" @click="deleteStudentHandler" size="mini">删除</el-button>
      </el-row>
      <div class="student-table">
        <el-table :data="studentsList" tooltip-effect="dark" style="width: 100%"
                  :row-class-name="rowStudentIndex" @selection-change="handleStudentChange">
          <el-table-column align="center" type="selection" width="50"></el-table-column>
          <el-table-column align="center" label="序号" prop="id" width="60">
          </el-table-column>
          <el-table-column align="center" prop="name" label="姓名" width="120">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" placeholder="请输入姓名"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="username" label="用户名" width="140">
            <template slot-scope="scope">
              <el-input v-model="scope.row.username" placeholder="请输入用户名"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="age" label="年龄" width="130">
            <template slot-scope="scope">
              <el-input v-model="scope.row.age" placeholder="请输入年龄"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="address" label="地址" width="130">
            <template slot-scope="scope">
              <el-input v-model="scope.row.address" placeholder="请输入地址"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="gender" label="性别" placeholder="请输入性别" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.gender" placeholder="请选择性别">
                <el-option label="男" value="0"></el-option>
                <el-option label="女" value="1"></el-option>
                <el-option label="未知" value="2"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="birthday" label="生日" width="150">
            <template slot-scope="scope">
              <el-date-picker
                style="width: 100%"
                v-model="scope.row.birthday"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择生日">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="enrollmentDate" label="入学日期" width="180">
            <template slot-scope="scope">
              <el-date-picker
                style="width: 100%"
                v-model="scope.row.enrollmentDate"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择入学日期">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="graduationDate" label="毕业日期" width="180">
            <template slot-scope="scope">
              <el-date-picker
                style="width: 100%"
                v-model="scope.row.graduationDate"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
                placeholder="请选择毕业日期">
              </el-date-picker>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="dialogSubmitHandler">确 定</el-button>
      </div>
    </el-dialog>

    <el-pagination
      background
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[5, 20, 50, 100]"
      :page-size="pageSize"
      layout="->,total, sizes, prev, pager, next, jumper"
      :total="totalPage">
    </el-pagination>

  </div>
</template>

<script>
import {deleteList, editList, pageList, studentInClass} from "@/api/campus/class";
import {style} from "svgo/lib/svgo/jsAPI";

let cs = 1;

export default {

  name: "index.vue",
  data() {
    return {
      time: '',
      classes: [],
      //　该班级下的students
      studentsList: [],
      selectClass: {
        name: '',
        code: ''
      },
      multipleSelection: [],
      ids: [],
      toggle: true,
      dialogVisible: false,
      dialogMethod: '',
      dialogTitle: '',
      dialogForm: {
        id: '',
        name: '',
        code: '',
        student: [],
      },
      pageNum: 1,
      pageSize: 5,
      totalPage: 0
    }
  },
  created() {
    this.pageListClass()
  },
  methods: {
    style() {
      return "background-color:#eee;color:#000;"
    },
    // axios渲染list
    // listClass(params) {
    //   getList(params).then((res) => {
    //     console.log(res)
    //     this.classes = res.data
    //   }).catch(() => {
    //     this.$message.error("查询失败")
    //   })
    // },

    // 复选 ids
    handleSelectionChange(val) {
      this.multipleSelection = val
      this.ids = this.multipleSelection.map(i => i.id)
    },
    handleStudentChange(val) {
      this.ids = val.map(i => i.id)
    },
    // 查询按钮
    selectButton() {
      this.pageListClass()
    },
    // 重置按钮
    reset() {
      this.selectClass = {}
      this.pageListClass()
    },
    insertHandler() {
      this.dialogMethod = 'post'
      this.dialogTitle = '新增班级'
      this.dialogVisible = true
      this.resetDialogForm()
    },
    updateRow(row) {
      this.dialogMethod = 'put'
      this.dialogTitle = '修改班级'
      this.dialogVisible = true
      this.dialogForm = JSON.parse(JSON.stringify(row))
      this.selectStudentInClass(row.id)
      this.resetDialogForm()
    },
    // 删除
    deleteHandler() {
      deleteList(this.ids).then(res => {
        this.$message.success("删除成功")
        this.pageListClass()
      })
    },
    deleteRow(row) {
      deleteList(row.id).then(res => {
        this.$message.success("删除成功")
        this.pageListClass()
      })
    },
    rowStudentIndex({row, rowIndex}) {
      row.id = rowIndex + 1
    },
    insertStudentHandler() {
      let obj = {}
      obj.username = ''
      obj.age = ''
      obj.name = ''
      obj.gender = ''
      obj.birthday = ''
      obj.enrollmentDate = ''
      obj.graduationDate = ''
      obj.address = ''
      obj.avatar = ''
      obj.resume = ''
      this.studentsList.push(obj)
    },
    deleteStudentHandler() {
      let tempList = []
      for (let i = 0; i < this.studentsList.length; i++) {
        if (!this.ids.includes(this.studentsList[i].id)) {
          tempList.push(this.studentsList[i])
        }
      }
      console.log(tempList)
      this.studentsList = tempList
    },
    // 重置弹框
    resetDialogForm() {
      this.dialogForm = this.$options.data().dialogForm
      this.studentsList = this.$options.data().studentsList
    },
    // 点击提交
    dialogSubmitHandler() {
      this.dialogForm.students = this.studentsList
      console.log(this.studentsList)
      editList(this.dialogMethod, this.dialogForm).finally(() => {
        this.dialogVisible = false
        this.pageListClass()
      })
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.pageListClass()
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.pageListClass()
      console.log(`当前页: ${val}`);
    },
    // 分页渲染list
    pageListClass() {
      pageList(this.pageNum, this.pageSize, this.selectClass).then((res) => {
        console.log(res)
        this.totalPage = res.data.count
        this.classes = res.data.list
      })
    },
    selectStudentInClass(id) {
      studentInClass(id).then(res => {
        this.studentsList = res.data.students
        this.dialogForm.name = res.data.name
        this.dialogForm.code = res.data.code
        this.dialogForm.id = res.data.id
        console.log(res.data)
      })
    }
  }
}
</script>

<style scoped lang="scss">
.right-toolbar {
  float: right;
}
</style>
